
package com.tpv.easychangelanguage;

import java.util.ArrayList;
import java.util.Locale;

import com.android.internal.app.LocalePicker;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private final static String TAG = "MainActivity";
    private ListView mLanguageList;
    private TextView mCurrentLanguageTxt;
    private LanguageListAdapter mLanguageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initAdapter();
        initResource();
        changeCurrentLanguageText();
    }

    private void initResource() {
        mLanguageList = (ListView) findViewById(R.id.list_language);
        mLanguageList.setOnItemClickListener(mLanguageListItemClickListener);
        if (mLanguageAdapter != null)
            mLanguageList.setAdapter(mLanguageAdapter);
        mCurrentLanguageTxt = (TextView) findViewById(R.id.current_language);
    }

    private void initAdapter() {
        mLanguageAdapter = new LanguageListAdapter(this);
        try {
            String[] languageName = getResources().getStringArray(R.array.language_name);
            String[] languageAbbreviation = getResources()
                    .getStringArray(R.array.language_abbreviation);
            ArrayList<LanguageInfo> languageInfoArray = new ArrayList<LanguageInfo>();
            for (int i = 0; i < languageName.length; i++) {
                LanguageInfo lan = new LanguageInfo();
                if (i < languageAbbreviation.length)
                    lan.setLanguageAbbreviation(languageAbbreviation[i]);
                lan.setLanguageName(languageName[i]);
                languageInfoArray.add(lan);
            }
            mLanguageAdapter.initLanguageInfoArray(languageInfoArray);
        } catch (Exception e) {
            Log.e(TAG, " initAdapter error = " + e.getMessage(), e);
        }
    }

    private OnItemClickListener mLanguageListItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            LanguageInfo lanInfo = (LanguageInfo) adapter.getItemAtPosition(position);
            setLanguage(lanInfo.getLanguageAbbreviation());
        }
    };

    private void setLanguage(String languageAbbreviation) {
        if (TextUtils.isEmpty(languageAbbreviation)) {
            Toast.makeText(this, R.string.invalid_language, Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            LocalePicker.updateLocale(Locale.forLanguageTag(languageAbbreviation));
            changeCurrentLanguageText();
            Toast.makeText(this, R.string.change_language_success, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e(TAG, " setLanguage() error = " + e.getMessage(), e);
            Toast.makeText(this, R.string.change_language_failed, Toast.LENGTH_SHORT).show();
        }
    }

    private void changeCurrentLanguageText() {
        Locale locale = getResources().getConfiguration().locale;
        String currentSysLan = locale.getLanguage();
        String country = locale.getCountry();
        if (!TextUtils.isEmpty(country))
            currentSysLan += "-" + country;// it is Abbreviation
        if (!TextUtils.isEmpty(currentSysLan) && mLanguageAdapter != null
                && mLanguageAdapter.getLanguageInfoArray() != null) {
            for (int i = 0; i < mLanguageAdapter.getLanguageInfoArray().size(); i++) {
                if (currentSysLan.equals(
                        mLanguageAdapter.getLanguageInfoArray().get(i).getLanguageAbbreviation())) {
                    String curLanText = getString(R.string.current_language,
                            mLanguageAdapter.getLanguageInfoArray().get(i).getLanguageName());
                    mCurrentLanguageTxt.setText(curLanText);
                    break;
                }
            }
        }
    }
}
